﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.General;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

namespace LEEAAttendanceControl.Views
{
    public partial class JobsEdit : ChildWindow
    {
        LEEAContext context = new LEEAContext();

        Job job = new Job();

        public JobsEdit()
        {
            InitializeComponent();
        }

        public JobsEdit(string title)
        {
            Title = title;

            InitializeComponent();
        }

        public void LoadData(int id)
        {
            context.Load(context.GetJobByIdQuery(id), OnJobLoadCompleted, null);
        }

        private void OnJobLoadCompleted(LoadOperation load)
        {
            job = (Job)load.Entities.FirstOrDefault();

            if (job == null)
            {
                context.GetJobMaxId(id =>
                    {
                        CustomIDBox.Text = id.Value.Value.ToString();
                    }
                    , null);

                job = new Job();

                context.Jobs.Add(job);
            }

            DataContext = job;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            context.SubmitChanges(OnChangedSubmitted, null);
        }

        protected void OnChangedSubmitted(SubmitOperation so)
        {
            if (so.HasError)
            {
                so.MarkErrorAsHandled();
            }
            else
            {
                MessageBox.Show(ApplicationStrings.GeneralSave, ApplicationStrings.GeneralSave, MessageBoxButton.OK);
                this.Close();
            }
        }
    }
}

